﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bzzb.Model;
using FreeSql.Internal.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Bzzb.Controllers
{
    public class BaoZhangSheBeiTypeController : BaseController
    {
        private readonly ILogger<BaoZhangSheBeiTypeController> _logger;
        public BaoZhangSheBeiTypeController(ILogger<BaoZhangSheBeiTypeController> logger, IFreeSql fsql) : base(fsql)
        {
            this._logger = logger;
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Index()
        {
            List<BaoZhangSheBeiType> baozhangshebeitypes = await _fsql.Select<BaoZhangSheBeiType>().Count(out var total).ToListAsync();
            _logger.LogInformation(_fsql.Select<BaoZhangSheBeiType>().ToSql());
            JObject data = new();
            data.Add("total", total);
            data.Add("list", JArray.FromObject(baozhangshebeitypes));
            return RenderJson(data: data);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Save([FromForm] BaoZhangSheBeiType baozhangshebeitype)
        {
            long id = await _fsql.Insert(baozhangshebeitype).ExecuteIdentityAsync();
            baozhangshebeitype.Id = id;

            return RenderJson(data: baozhangshebeitype);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Delete([FromForm] long id) {
            int affrows = await _fsql.Delete<BaoZhangSheBeiType>().Where(bzsbt => bzsbt.Id == id).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Update([FromForm] BaoZhangSheBeiType baozhangshebeitype) {
            int affrows = await _fsql.Update<BaoZhangSheBeiType>(baozhangshebeitype.Id).Set(bzsbt => bzsbt.Fieldinfo, baozhangshebeitype.Fieldinfo).ExecuteAffrowsAsync();
            BaoZhangSheBeiType data = await _fsql.Select<BaoZhangSheBeiType>().Where(bzsbt => bzsbt.Id == baozhangshebeitype.Id).ToOneAsync();
            return RenderJson(data: data);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Show([FromForm] long id) {
            BaoZhangSheBeiType baozhangshebeitype = await _fsql.Select<BaoZhangSheBeiType>().Where(bzsbt => bzsbt.Id == id).ToOneAsync();
            return RenderJson(data: baozhangshebeitype);
        }
    }
}
